package com.chen.controller;

import com.chen.dto.CourseDTO;
import com.chen.service.CourseService;
import com.chen.service.GradeService;
import com.chen.vo.CourseVO;
import com.chen.vo.GradeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 科目管理相关接口
 */
@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    // 年级
    @Autowired
    private GradeService gradeService;

    /**
     * 查看科目信息
     * @param gradeId 年级编号
     * @param division 分科情况
     * @return
     */
    @RequestMapping("/courses")
    public ModelAndView getCourses(@RequestParam(value="gradeId", required=false) Integer gradeId,
                                   @RequestParam(value="division", required=false) Integer division) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/courses");
        List<CourseVO> courses = courseService.listCourses(gradeId, division);
        model.addObject("courses", courses);
        return model;
    }

    /**
     * 新增科目页面
     * @return
     */
    @GetMapping("/preAddCourse")
    public ModelAndView preAddCourse() {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/courseAdd");
        /** 获取年级集合 */
        List<GradeVO> grades = gradeService.findAllGrade();
        model.addObject("grades", grades);
        return model;
    }

    /**
     * 新增科目信息
     * @param dto
     * @return
     */
    @PostMapping("/save")
    public String save(CourseDTO dto) {
        courseService.saveCourse(dto);
        return "redirect:/course/courses";
    }

    /**
     * 进入修改学科页面
     * @param courseId
     * @return
     */
    @GetMapping("/edit/{courseId}")
    public ModelAndView edit(@PathVariable("courseId") Integer courseId) {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/courseedit");
        /** 获取年级集合 */
        List<GradeVO> grades = gradeService.findAllGrade();
        model.addObject("grades", grades);
        CourseVO course = courseService.getCourseById(courseId);
        model.addObject("course", course);
        return model;
    }

    /**
     * 修改科目西悉尼
     * @param dto
     * @return
     */
    @PostMapping("/update")
    public String update(CourseDTO dto) {
        courseService.updateCourse(dto);
        return "redirect:/course/courses";
    }


    /**
     * 删除学科
     * @param courseId 删除学科
     * @return
     */
    @PostMapping(value="/del/{courseId}")
    public String delCourse(@PathVariable("courseId") Integer courseId) {
        courseService.delCourse(courseId);
        return "redirect:/course/courses";
    }


}
